<!-- 本示例未包含完整css，获取外链css请参考上文，在hello uni-app项目中查看 -->
<template>
	<view class="bg-white">
		<titleBar title="修改密码" isReturn="true"></titleBar>
		<view class="padding-top">
			<!-- 	<uni-forms>  

				<uni-forms-item>
					<uni-easyinput placeholder="请输入密码"></uni-easyinput>
				</uni-forms-item>
			</uni-forms> -->

			<uni-forms ref="form" :value="formData" :rules="rules" class="padding-top">

				<uni-forms-item label="旧密码" name="oldPwd" labelAlign="left" class="margin-lr-sm" labelWidth="75px">

					<uni-easyinput type="password" v-model="formData.oldPwd" placeholder="请输入旧密码"
						class="margin-lr-sm" />
				</uni-forms-item>
				<uni-forms-item label="新密码" name="newPwd" labelAlign="left" labelWidth="75px">
					<uni-easyinput type="password" v-model="formData.newPwd" placeholder="请输入新密码" />
				</uni-forms-item>
				<uni-forms-item label="确认密码" name="Pwd" labelAlign="left" labelWidth="75px">
					<uni-easyinput type="password" v-model="formData.Pwd" placeholder="请确认密码" />
				</uni-forms-item>


			</uni-forms>

		</view>
		<view class=" padding flex flex-direction bg-white">
			<button class="cu-btn bg-gradual-blue lg" @tap="submit">修改</button>
		</view>



	</view>
</template>
<script>
	import titleBar from '@/components/titleBar/titleBar.vue'

	export default {
		components: {

			titleBar,

		},


		data() {
			return {
				changePwdUrl: '/sys/user/updatePassword',
				changePwdMsg: {
					username: '',
					oldpassword: '',
					password: '',
					confirmpassword: ''
				},
				formData: {
					oldPwd: '',
					newPwd: '',
					Pwd: ''
				},
				rules: {
					// 对oldPwd字段进行必填验证
					oldPwd: {
						rules: [{
								required: true,
								errorMessage: '请输入旧密码',
							}

						]
					},
					// 对newPwd字段进行必填验证
					newPwd: {
						rules: [{
							required: true,
							errorMessage: '请输入新密码',
						}]
					},
					// 对Pwd字段进行必填验证
					Pwd: {
						rules: [{
								required: true,
								errorMessage: '请确认密码',
							}, {

								validateFunction: function(rule, value, data, callback) {
									
									if (data.newPwd != data.Pwd) {
										// 通过返回 resolve
										callback('密码输入不一致')

									}
									return true
								}
							}

						]
					}
				},
			}
		},
		onLoad(option) {
			
			this.changePwdMsg.userName = uni.getStorageSync("userName")

		},
		onReady() {
			// 需要在onReady中设置规则
			// 使用 validateFunction 自定义校验规则,则不能采用 uni-forms 的 rules 属性来配置校验规则，这时候需要通过ref，在onReady生命周期调用组件的setRules方法绑定验证规则
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			submit() {

				//验证表单
				this.$refs.form.submit().then(res => {


					//修改密码
					uni.showModal({
						title: '提示',
						content: '确定要修改密码吗？',
						cancelText: '取消',
						confirmText: '确定',
						success: res => {
							if (res.confirm) {
								
								this.$refs.form.submit().then(res => {  
									
									this.changePwdMsg.oldpassword = res.oldPwd
									this.changePwdMsg.password = res.newPwd
									this.changePwdMsg.confirmpassword = res.Pwd
									
									
									if (res.newPwd != res.Pwd) {
										uni.showToast({
											title: "新密码不一致",
											icon: 'none',
											duration: 2000
										});
										return false;
									} else {
										
										
										var that = this
										
										uni.request({
										
											url: "https://www.turkeylike.cn:8900/fzulab/sys/mPassword",
											method: 'post',
											data: {
												userName: this.changePwdMsg.userName,
												oldpassword:this.changePwdMsg.oldpassword,
												password: this.changePwdMsg.password,
										
											}, 
										 
										
											success(res) {
												
												
												if (res.data.code == '500') {
													uni.showToast({
														title: res.data.msg,
														icon: 'none',
														duration: 2000
													});
													that.loading = false;
													return
												}
												
												uni.showToast({
													title: "修改密码成功！",
													icon: 'none',
													duration: 2000
												});
												uni.switchTab({
													url: '/pages/home/home',
													success: function(e) {
														// pop() 方法用于删除数组其他元素并返回数组的最后一个元素
														let currentPage = getCurrentPages().pop(); //当前页面
														if (currentPage == undefined || currentPage == null) return;
														currentPage.onLoad(); //页面存在刷新当前页面
														//currentPage.onShow()
														//currentPage.onReady()
														// 或者执行其他生命周期刷新
													},
												});
										
											
										
											}
										})
										
										
										
										
										
										// this.$http.put(this.changePwdUrl, this.changePwdMsg)
										// 	.then(res => {
										// 		console.log("res", res)
										// 		if (res.data.success) {
										// 			let perArr = res.data.result
										// 			// this.recordDetilList.picture = 'http://192.168.1.211:8080/jeecg-boot/' + perArr.picture
										// 			uni.showToast({
										// 				title: '修改成功，请返回登录',
										// 				icon: 'none',
										// 				success: function(res) {
										// 					//清除缓存

										// 					uni.removeStorageSync(
										// 						'diaryMsg')
										// 					setTimeout(() => {
										// 						uni.reLaunch({
										// 							url: '/pages/common/exit'
										// 						})
										// 					}, 500)
										// 				}
										// 			})
										// 		} else {
										// 			uni.showToast({
										// 				title: res.data.message +
										// 					',请重新输入',
										// 				icon: 'none',
										// 			})

										// 			this.formData.oldPwd = ''
										// 			this.formData.Pwd = ''
										// 			this.formData.newPwd = ''
										// 		}
										// 	}).catch(err => {
										// 		console.log(err);
										// 	});
									}

								}).catch(err => {

									console.log('表单错误信息：', err);
								})

							}
						}
					})
				}).catch(err => {

					console.log('表单错误信息：', err);

				})








			}
		}
	}
</script>

<style>
	.uni-form-item .title {
		padding: 200rpx 0;
	}
</style>
